// packageB/pages/meiri/meiri.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    appurl: app.apiurl,
    apiimg: app.apiimg,
    ggid: '',
    gglist: '',
    gglist1: '',
    tanchuang1:false,
    tanchuang2:false,
    tanchuang3:false,
    tanchuang4:false,
    tanchuang5:false,
    tanchuang6:false,
    list:'',
    qiandao:'',
    rw_id:'',
    list_pic:'',
    peizhi:"",
    list_pic1:false,
    weidenglu:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.z_peizhi()
    if (wx.getStorageSync("user_token") == "") {
      this.setData({
        weidenglu:true
      })
    } else {
      this.setData({
        weidenglu:false
      })
    }
  },
  z_dengluyanzeng(){
    wx.request({
      url: app.apiurl + 'api/home/xcxLoginStatus',
      method: 'GET',
      data: {},
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        if (res.data.code == '1001') {
          if (res.data.data.is_force_login == '1') {
            // wx.reLaunch({
            //   url: '/pages/login/login'
            // });
            wx.showToast({
              title: '请注册/登录会员账号',
              icon: 'none',
              duration: 2000
            })
            setTimeout(function(){
              wx.reLaunch({
                url: '/pages/member/member',
              })
            },2500)
          }
        }
      }
    })
  },
  z_upload() {
    let that = this
    let num = 6 - Number(that.data.list.length)
    if (num != 0) {
      wx.chooseImage({
        // 最多可以选择的图片张数
        count: num,
        sizeType: ["compressed"],
        //   选择图片的来源
        sourceType: ["album", "camera"],
        success: function (res) {
          var shangchuan = true;
          for (let i = 0; i < res.tempFilePaths.length; i++) {
            var tempFiles = res.tempFilePaths[i];
            var fileSize = res.tempFiles[i]['size'];
            var MAX_SIZE = Number(that.data.peizhi.config_param39) * 1024 * 1024;
            if (fileSize > MAX_SIZE) {
              wx.showToast({
                title: '文件超过大小限制',
                icon: 'none',
              });
              shangchuan = false;
              // 文件超过限制，处理逻辑，比如重新选择
              return;
            }
          }
          that.setData({
            temppath: res.tempFilePaths
          })
          if (shangchuan) {
            setTimeout(function () {
              that.z_shangchuan()
            }, 1000)
          }
        },
      })
    }
  },
  z_shangchuan() {
    let _this = this
    const filePaths = _this.data.temppath;
    filePaths.forEach((filePath, index) => {
      wx.uploadFile({
        url: 'https://www.zonghengxin.com/api/common/upload',
        filePath: filePath,
        name: 'file',
        formData: {},
        // 超时时间，单位为毫秒
        timeout: 600000000,
        header: {
          "content-type": "multipart/form-data"
        },
        success: (res) => {
          const data = JSON.parse(res.data)
          _this.setData({
            list_pic: data.data.fullurl,
            list_pic1:true,
            jietu2:false
          })
          _this.z_renwutijiao()
        },
        fail: (err) => {
          console.error(`第 ${index + 1} 个文件上传失败`, err);
        },
        complete: () => {
          // 可选：所有文件上传完成后的操作（需自行判断是否全部完成）
        }
      });
    });
  },
  z_renwutijiao(){
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token"),
      "rw_id":_this.data.rw_id,
      "pic":_this.data.list_pic
    }
    wx.request({
      url: app.apiurl + 'api/season/jtTaskAdd',
      method: 'POST',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        _this.z_jilu()
      }
    })
  },
  z_open(){
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token"),
      "sign_type":"2"
    }
    wx.request({
      url: app.apiurl + 'api/season/memberSign',
      method: 'POST',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        _this.z_jilu()
        _this.setData({
          tanchuang4:true,
          qiandao:res.data.data
        })
      }
    })
  },
  z_close(){
    this.setData({
      tanchuang1:false,
      tanchuang2:false,
      tanchuang3:false,
      tanchuang4:false,
      tanchuang5:false,
      jietu2:false,
      list: ''
    })
    this.z_jilu()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.guanggao1()
    this.z_yonghu()
    this.z_jilu()
    this.z_peizhi()
  },
  z_peizhi(){
    let _this = this
    let params = {
      "token":wx.getStorageSync("user_token"),
    }
    wx.request({
      url: app.apiurl + 'api/config/configBase',
      method: 'GET',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        _this.setData({
          peizhi: res.data.data
        })
        let show_liaoliao = res.data.data.config_param55
        if(show_liaoliao == '1'){
          _this.setData({
            show_liaoliao_flag:true
          })
        }
      }
    })
  },
  z_yonghu() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token"),
    }
    wx.request({
      url: app.apiurl + 'api/member/myInfo',
      method: 'POST',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        if (res.data.code == '1001') {
          _this.setData({
            login: true,
            yonghu: res.data.data
          })
        }
      }
    })
  },
  guanggao1() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token"),
    }
    wx.request({
      url: app.apiurl + 'api/config/advertPage',
      method: 'POST',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        console.log(res.data.data)
        _this.setData({
          ggid: res.data.data[0].id
        })
        _this.guanggao2()
        _this.guanggao2()
      }
    })
  },
  guanggao2() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token"),
    }
    wx.request({
      url: app.apiurl + 'api/config/advert?page_id=19',
      method: 'GET',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        _this.setData({
          gglist: res.data.data[0]
        })
      }
    })
  },
  guanggao3() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token"),
    }
    wx.request({
      url: app.apiurl + 'api/config/advert?page_id=23',
      method: 'GET',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        _this.setData({
          gglist1: res.data.data[0]
        })
      }
    })
  },
  z_kvlianjia(e){
    let _this = this
    let link_url = e.currentTarget.dataset.url
    let id = e.currentTarget.dataset.url
    if(link_url != undefined){
      wx.navigateTo({
        url: link_url,
      })
    }
  },
  z_opem1(e){
    let _this = this
    let rw_id = e.currentTarget.dataset.rw_id
    _this.setData({
      rw_id:rw_id,
      tanchuang2:true,
      list_pic1:false
    })
  },
  z_opem2(e){
    let _this = this
    let rw_id = e.currentTarget.dataset.rw_id
    _this.setData({
      rw_id:rw_id,
      tanchuang1:true,
      list_pic1:true
    })
  },
  z_opem3(e){
    let _this = this
    let rw_id = e.currentTarget.dataset.rw_id
    let rwjt_url = e.currentTarget.dataset.src
    _this.setData({
      rw_id:rw_id,
      tanchuang2:true,
      list_pic1:false,
      jietu1:rwjt_url,
      jietu2:true
    })
  },
  z_jilu() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token"),
    }
    wx.request({
      url: app.apiurl + 'api/treasure/taskInfo',
      method: 'GET',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        _this.setData({
          list: res.data.data
        })
      }
    })
  },
  z_xinrenyindao5() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token"),
      "five_yindao":'5'
    }
    wx.request({
      url: app.apiurl + 'api/member/upMyYd2',
      method: 'POST',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        _this.z_yonghu()
        wx.reLaunch({
          url: '/pages/index/index',
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})